package com.faithnmn.learnenglishbc.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.faithnmn.learnenglishbc.R;
import com.faithnmn.learnenglishbc.service.LessionsPlayer;
import com.faithnmn.learnenglishbc.util.CommonUtil;
import com.faithnmn.utils.core.model.Configurations;

public class StableArrayAdapter extends ArrayAdapter<Configurations> {
	private ArrayList<Configurations> configurations;
	private LayoutInflater inflater;
	private boolean isVietnamese = true;
	private boolean showSavedIndicator = false;
	private boolean showPlayingIcon = false;
	private int currentSeriIndex = 0;
	private ArrayList<Boolean> savedList = new ArrayList<Boolean>();
    public StableArrayAdapter(Context context,LayoutInflater inflater, int textViewResourceId,
        ArrayList<Configurations> objects,MenuItemClickListener itemClickListener) {
      super(context, textViewResourceId, objects);
      this.configurations = objects;
      this.inflater = inflater;
    }
    
    public void setShowSavedIndicator(boolean showSavedIndicator){
    	this.showSavedIndicator = showSavedIndicator;
    }
    
    public void setShowPlayingIcon(boolean showPlayingIcon,int currentSeriIndex){
    	this.showPlayingIcon = showPlayingIcon;
    	this.currentSeriIndex = currentSeriIndex;
    }
    
    public void setIndexSeries(int indexSeries){
    	for(int i=0;i<12;i++){
    		savedList.add(CommonUtil.isAudioAvailable(indexSeries, i));
    	}
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    	ViewHolder holder;
    	if(convertView == null){
    		convertView = inflater.inflate(R.layout.app_item_info, null);
    		holder = new ViewHolder();
    		holder.appTitle = (TextView) convertView.findViewById(R.id.app_title);
    		holder.appDescription = (TextView)convertView.findViewById(R.id.app_desciption);
    		holder.showSavedIndicator = (TextView)convertView.findViewById(R.id.savedIndicator);
    		holder.playingIcon = (ImageView)convertView.findViewById(R.id.playing_icon);
    		convertView.setTag(holder);
    	}else{
    		holder = (ViewHolder) convertView.getTag();
    	}
    	if(isVietnamese){
    		holder.appTitle.setText(configurations.get(position).udTitle);
    		holder.appDescription.setText(configurations.get(position).description);
    	}else{
    		holder.appTitle.setText(configurations.get(position).udTitle_en);
    		holder.appDescription.setText(configurations.get(position).description_en);
    	}
    	if(showSavedIndicator){
    		if(savedList.size()>= position && savedList.get(position)){
    			holder.showSavedIndicator.setVisibility(View.VISIBLE);
    			holder.showSavedIndicator.setText("\u0110\u00E3 l\u01B0u!");
    			holder.showSavedIndicator.setBackgroundColor(Color.parseColor("#aae037"));
    		}else{
    			holder.showSavedIndicator.setVisibility(View.GONE);
    		}
    	}else{
    		holder.showSavedIndicator.setVisibility(View.GONE);
    	}
    	if(showPlayingIcon){
    		if(LessionsPlayer.currentLessionIndex == position && LessionsPlayer.currentSeriIndex == currentSeriIndex){
    			holder.playingIcon.setVisibility(View.VISIBLE);
    		}else{
    			holder.playingIcon.setVisibility(View.GONE);
    		}
    	}
    	return convertView;
    }

    @Override
    public long getItemId(int position) {
      Configurations item = configurations.get(position);
      return Long.parseLong(item.udPId);
    }

    @Override
    public int getCount() {
    	return configurations.size();
    }
    
    @Override
    public boolean hasStableIds() {
      return true;
    }
    
    private class ViewHolder{
    	private TextView appTitle;
    	private TextView appDescription;
    	private TextView showSavedIndicator;
    	private ImageView playingIcon;
    }

	public void refreshData(ArrayList<Configurations> configurations) {
		this.configurations = configurations;
		notifyDataSetChanged();
	}
    
	public static interface MenuItemClickListener{
		public void onClickItemPosition(int postion);
	}
	
	public Configurations getLessionInfo(int position){
		return configurations.get(position);
	}
	
  }